package com.example.android_demo_2;

import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.CookieStore;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;

public class MainActivity extends Activity {

	Button btnJSONObjReq;
	Button btnStringReq;
	Button btnPostParam;
	Button btnPostJSONParam;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		CookieManager cookieManager = new CookieManager(null, CookiePolicy.ACCEPT_ALL);
		CookieHandler.setDefault(cookieManager);
		initBtnJSONObjReq();
		initBtnStringReq();
		initBtnPostParam();
		initBtnPostJSONParam();
	}

	private void initBtnJSONObjReq() {
		this.btnJSONObjReq = (Button) this.findViewById(R.id.btnJSONObjReq);
		this.btnJSONObjReq.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String tab_json_obj = "json_obj_req";
				String url = "http://api.androidhive.info/volley/person_object.json";
				JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
						Method.GET, url, null,
						new Response.Listener<JSONObject>() {
							@Override
							public void onResponse(JSONObject response) {
								Toast.makeText(getApplicationContext(),
										response.toString(), Toast.LENGTH_LONG)
										.show();
							}
						}, new Response.ErrorListener() {
							public void onErrorResponse(
									com.android.volley.VolleyError error) {
								Toast.makeText(getApplicationContext(),
										error.getMessage(), Toast.LENGTH_LONG)
										.show();
							};
						});
				VolleySingleton.getInstance().addToRequestQueue(
						jsonObjectRequest, tab_json_obj);
			}
		});
	}

	private void initBtnStringReq() {
		this.btnStringReq = (Button) this.findViewById(R.id.btnStringReq);
		this.btnStringReq.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String tab = "string_req";
				String url = "http://api.androidhive.info/volley/string_response.html";
				url = "https://www.google.com/";
				StringRequest stringRequest = new StringRequest(Method.GET,
						url, new Response.Listener<String>() {
							@Override
							public void onResponse(String response) {
								Toast.makeText(getApplicationContext(),
										response, Toast.LENGTH_LONG).show();
							}
						}, new Response.ErrorListener() {
							public void onErrorResponse(
									com.android.volley.VolleyError error) {
								Toast.makeText(getApplicationContext(),
										error.getMessage(), Toast.LENGTH_LONG)
										.show();
							};
						});
				VolleySingleton.getInstance().addToRequestQueue(stringRequest,
						tab);
			}
		});
	}

	private void initBtnPostParam() {
		this.btnPostParam = (Button) this.findViewById(R.id.btnPostParam);
		this.btnPostParam.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String tab = "post_param";
				String url = "http://api.androidhive.info/volley/person_object.json";
				StringRequest jsonObjectRequest = new StringRequest(
						Method.POST, url,
						new Response.Listener<String>() {
							@Override
							public void onResponse(String response) {
								Toast.makeText(getApplicationContext(),
										response.toString(), Toast.LENGTH_LONG)
										.show();
							}
						}, 
						new Response.ErrorListener() {
							public void onErrorResponse(
									com.android.volley.VolleyError error) {
								Toast.makeText(getApplicationContext(),
										error.getMessage(), Toast.LENGTH_LONG)
										.show();
							};
						}) 
				{
					@Override
					protected Map<String, String> getParams()
					{
						Map<String, String> params = new HashMap<String, String>();
						params.put("name", "Mai Tan");
						params.put("email", "Maitan@yahoo.com");
						params.put("password", "123");
						return params;
					}
				};
				VolleySingleton.getInstance().addToRequestQueue(
						jsonObjectRequest, tab);
			}
		});
	}
	
	private void initBtnPostJSONParam() {
		this.btnPostJSONParam = (Button) this.findViewById(R.id.btnPostJsonParam);
		this.btnPostJSONParam.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String tab = "post_JSON_param";
				String url = "http://api.androidhive.info/volley/person_object.json";
				JSONObject jsonObject = new JSONObject();
				try {
					jsonObject.put("name", "Mai Tan JSON");
					jsonObject.put("email", "Maitan@yahoo.com");
					jsonObject.put("password", "123");
				} catch (JSONException e) {
					e.printStackTrace();
				}
				
				JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
						Method.POST, url,jsonObject,
						new Response.Listener<JSONObject>() {
							@Override
							public void onResponse(JSONObject response) {
								Toast.makeText(getApplicationContext(),
										response.toString(), Toast.LENGTH_LONG)
										.show();
							}
						}, 
						new Response.ErrorListener() {
							public void onErrorResponse(
									com.android.volley.VolleyError error) {
								Toast.makeText(getApplicationContext(),
										error.getMessage(), Toast.LENGTH_LONG)
										.show();
							};
						});
				VolleySingleton.getInstance().addToRequestQueue(
						jsonObjectRequest, tab);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
